<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>阿巴阿巴</title>
    <link rel="stylesheet" href="a.css" type="text/css">
</head>
<body>
<div id="wrap">
    <h2>排行榜</h2>
    <div class="container">
        <table>
            <thead>
            <tr>
                <th>ID</th>
                <th>分数</th>
                <th>总跳跃</th>
                <th>跳跃仙人掌次数</th>
                <th>躲避翼龙次数</th>
            </tr>
            </thead>
            <tbody>
            <%
                int rowCount = 0;
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dino", "root", "123456");
                    String sql = "SELECT * FROM score ORDER BY Score DESC LIMIT 10";
                    PreparedStatement statement = connection.prepareStatement(sql);
                    ResultSet resultSet = statement.executeQuery();
                    while (resultSet.next()){
                        rowCount++;
                        int id = resultSet.getInt("id");
                        int score = resultSet.getInt("Score");
                        int all_jump = resultSet.getInt("All_jump");
                        int jump_catus = resultSet.getInt("jump_catus");
                        int avoid_pterosaur = resultSet.getInt("avoid_pterosaur");
            %>
            <tr>
                <td><%= id %></td>
                <td><%= score %></td>
                <td><%= all_jump %></td>
                <td><%= jump_catus %></td>
                <td><%= avoid_pterosaur %></td>
            </tr>
            <%
                    }
                    // 关闭连接和声明
                    resultSet.close();
                    statement.close();
                    connection.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // 如果数据行不足10行，用占位符填充
                while (rowCount < 10) {
                    rowCount++;
            %>
            <tr>
                <td colspan="5" style="text-align: left;">暂无数据</td>
            </tr>
            <%
                }
            %>
            </tbody>
        </table>
    </div>
    <script src="a.js"></script>
</div>
</body>
</html>
